package com.yanqu.road.dao.impl.activity.hsisland;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandUnionData;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class HsIslandUnionDataDaoImpl extends TempDataObjectDao<HsIslandUnionData> {

    public static String getHsIslandUnionDataSql = "select * from `t_c_activity_hs_island_union_data` where `activityId`=? and `groupId`=?";
    public Map<String, HsIslandUnionData> getHsIslandUnionMap(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            Map<String, HsIslandUnionData> map = new ConcurrentHashMap<>();
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getHsIslandUnionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                HsIslandUnionData data = parse(rs);
                map.put(data.getUnionId(), data);
            }
            return map;
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
    }

    public static String updateHsIslandUnionDataSql = "update `t_c_activity_hs_island_union_data` set maxFloorId=?,level=?,exp=?,dAllDonateNum=?,dUsableDonateNum=?,createTime=? where activityId=? and groupId=? and unionId=?";
    @Override
    protected boolean updateData(HsIslandUnionData hsIslandUnionData) {
        hsIslandUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUnionData.getMaxFloorId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUnionData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUnionData.getExp()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUnionData.getDAllDonateNum()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUnionData.getDUsableDonateNum()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUnionData.getCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUnionData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hsIslandUnionData.getUnionId()));
        if (getDb().executeNoneQuery(updateHsIslandUnionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHsIslandUnionDataSql = "insert into `t_c_activity_hs_island_union_data`(activityId,groupId,unionId,maxFloorId,level,exp,dAllDonateNum,dUsableDonateNum,createTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(HsIslandUnionData hsIslandUnionData) {
        hsIslandUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUnionData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hsIslandUnionData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUnionData.getMaxFloorId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUnionData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUnionData.getExp()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUnionData.getDAllDonateNum()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUnionData.getDUsableDonateNum()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUnionData.getCreateTime()));
        if (getDb().executeNoneQuery(insertHsIslandUnionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HsIslandUnionData parseData(ResultSet rs) throws SQLException {
        HsIslandUnionData hsIslandUnionData = new HsIslandUnionData();
        hsIslandUnionData.setActivityId(rs.getInt("activityId"));
        hsIslandUnionData.setGroupId(rs.getInt("groupId"));
        hsIslandUnionData.setUnionId(rs.getString("unionId"));
        hsIslandUnionData.setMaxFloorId(rs.getInt("maxFloorId"));
        hsIslandUnionData.setLevel(rs.getInt("level"));
        hsIslandUnionData.setExp(rs.getLong("exp"));
        hsIslandUnionData.setDAllDonateNum(rs.getInt("dAllDonateNum"));
        hsIslandUnionData.setDUsableDonateNum(rs.getInt("dUsableDonateNum"));
        hsIslandUnionData.setCreateTime(rs.getLong("createTime"));
        return hsIslandUnionData;
    }
}
